﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebSite.Common.Models.ViewModels;


namespace WebSite.Binders
{
    public class ClassesScheduleBinder: DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            if (bindingContext.ModelType == typeof(ClassesSchedule))
            {
                HttpRequestBase request = controllerContext.HttpContext.Request;

                string anyTimeCheck = request.Form.Get("Schedule.AnyTime");

                string mondayCheck = request.Form.Get("Schedule.Monday");
                string tuesdayCheck = request.Form.Get("Schedule.Tuesday");
                string wednesdayCheck = request.Form.Get("Schedule.Wednesday");
                string thursdayCheck = request.Form.Get("Schedule.Thursday");
                string fridayCheck = request.Form.Get("Schedule.Friday");
                string santurdayCheck = request.Form.Get("Schedule.Saturday");
                string sundayCheck = request.Form.Get("Schedule.Sunday");


                string mondayVal = request.Form.Get("Schedule.SelectMonday");
                string tuesdayVal = request.Form.Get("Schedule.SelectTuesday");
                string wednesdayVal = request.Form.Get("Schedule.SelectWednesday");
                string thursdayVal = request.Form.Get("Schedule.SelectThursday");
                string fridayVal = request.Form.Get("Schedule.SelectFriday");
                string santurdayVal = request.Form.Get("Schedule.SelectSaturday");
                string sundayVal = request.Form.Get("Schedule.SelectSunday");


                ClassesSchedule result = new ClassesSchedule();
                if (anyTimeCheck != "true,false")
                {
                    if (mondayCheck == null)
                        result.SetAvailableTime(DayOfWeek.Monday, LessonsTime.Unavailable);
                    else
                        result.SetAvailableTime(DayOfWeek.Monday, (LessonsTime)Enum.Parse(typeof(LessonsTime), mondayVal));

                    if (tuesdayCheck == null)
                        result.SetAvailableTime(DayOfWeek.Tuesday, LessonsTime.Unavailable);
                    else
                        result.SetAvailableTime(DayOfWeek.Tuesday, (LessonsTime)Enum.Parse(typeof(LessonsTime), tuesdayVal));
                    
                    if (wednesdayCheck == null)
                        result.SetAvailableTime(DayOfWeek.Wednesday, LessonsTime.Unavailable);
                    else
                        result.SetAvailableTime(DayOfWeek.Wednesday, (LessonsTime)Enum.Parse(typeof(LessonsTime), wednesdayVal));
                    
                    if (thursdayCheck== null)
                        result.SetAvailableTime(DayOfWeek.Thursday, LessonsTime.Unavailable);
                    else
                        result.SetAvailableTime(DayOfWeek.Thursday, (LessonsTime)Enum.Parse(typeof(LessonsTime), thursdayVal));

                    if (fridayCheck== null)
                        result.SetAvailableTime(DayOfWeek.Friday, LessonsTime.Unavailable);
                    else
                        result.SetAvailableTime(DayOfWeek.Friday, (LessonsTime)Enum.Parse(typeof(LessonsTime), fridayVal));

                    if (santurdayCheck == null)
                        result.SetAvailableTime(DayOfWeek.Saturday, LessonsTime.Unavailable);
                    else
                        result.SetAvailableTime(DayOfWeek.Saturday, (LessonsTime)Enum.Parse(typeof(LessonsTime), santurdayVal));

                    if (sundayCheck == null)
                        result.SetAvailableTime(DayOfWeek.Sunday, LessonsTime.Unavailable);
                    else
                        result.SetAvailableTime(DayOfWeek.Sunday, (LessonsTime)Enum.Parse(typeof(LessonsTime), sundayVal));
                }
                return result;
            }
            else
            {
                return base.BindModel(controllerContext, bindingContext);
            }
        } 
    }
}